const app = getApp()
import api from '../../utils/api'
Page({

  /**
   * 页面的初始数据
   */
  data: {
    navBarHeight: app.globalData.navBarHeight, //导航栏高度
    menuBotton: app.globalData.menuBotton, //导航栏距离顶部距离
    menuHeight: app.globalData.menuHeight, //导航栏高度

    imgUploading: false,
    moduleType: "", //预设当前项的值
    pics: [],
    isShow: true,
    imgUrl: [],
    title: "",
    module: 0,
    submitFlag: true
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {

  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  },
  
  /**
   * 返回上一页
   */
  goBack: function (e) {
    wx.navigateBack({
      delta: 1,
    })
  },
  changeModuleType: function(e) {
    this.setData({
      moduleType: e.currentTarget.dataset.type
    })
    if (this.data.module != e.currentTarget.dataset.module) {
      this.setData({
        module: e.currentTarget.dataset.module,
        moduleType: e.currentTarget.dataset.type
      })
    } else {
      this.setData({
        module: 0,
        moduleType: ""
      })
    }
  },

  feedbackInput: function (e) {
    if(e.detail.value.length <= 140) {
      this.setData({
        title: e.detail.value
      })
    }
  },

  uploadImage: function () {
    let that = this;
    let pics = that.data.pics;
    let picsUpload = [];
    let imgUrl = that.data.imgUrl;

    wx.chooseImage({
      //限定上传张数x
      // count: 3 - pics.length,
      count: 3 - pics.length,
      sizeType: ['original', 'compressed'],
      sourceType: ['album', 'camera'],
      success: function (res) {
        let imgSrc = res.tempFilePaths;
        pics = pics.concat(imgSrc);
        picsUpload = picsUpload.concat(imgSrc);
        //限定上传张数=3
        if (pics.length >= 3) {
          that.setData({
            isShow: false
          })
        }
        //显示等待框
        that.setData({
          pics: pics,
          waitHidden: false,
          imgUploading: true
        })
        wx.showLoading({
          title: '图片上传中',
        })
        //调取图片上传
        for (var i = 0; i < picsUpload.length; i++) {
          wx.uploadFile({
            //对象转字符串.join("")
            filePath: picsUpload[i],
            name: 'img',
            url: api.uploadImg,
            success: function (res) {
              wx.hideLoading()
              if(res.data.toString().includes("Too Large")) {
                wx.showToast({
                  title: '图片超过1M',
                  icon: 'error'
                })
              } else {
                var data = JSON.parse(res.data)
                if(data.data!=null){
                  that.data.imgUrl.push(data.data.imgUrl)
                  that.setData({
                    imgUrl: that.data.imgUrl
                  })
                }
                if (data.state == "1") {
                  wx.showToast({
                    title: '图片上传成功',
                  })
                } else if (data.state == "2") {
                  wx.showToast({
                    title: '图片上传失败，请重新上传或选择分辨率较小的图片',
                    icon: 'error'
                  })
                }
              }
              that.setData({
                imgUploading: false
              })
            },
            fail: function (res) {
              wx.showToast({
                title: '图片上传失败，请重新上传或选择分辨率较小的图片',
                icon: 'error'
              })
            },
            complete: () => {
              // console.log("complete")
            }
          })
        }
        // 设置图url
        that.setData({
          imgUrl: that.data.imgUrl,
          //隐藏等待框
          waitHidden: true
        })
      },
    })
  },

  deleteImg: function (e) {
    var pics = this.data.pics;
    var imgUrl = this.data.imgUrl
    var index = e.currentTarget.dataset.index;
    pics.splice(index, 1);
    imgUrl.splice(index, 1);
    this.setData({
      pics: pics,
      imgUrl: imgUrl,
      isShow: true
    })
    // //图片url置空
    // that.setData({
    //   imgUrl: ""
    // })
  },

  tapToSubmit: function (e) {
    var that = this
    if (this.data.title == "") {
      wx.showToast({
        title: '请输入反馈内容',
        icon: 'error'
      })
    } else if (that.data.moduleType == "") {
      wx.showToast({
        title: '请选择反馈类型',
        icon: 'error'
      })
    } else if (that.data.imgUploading) {
      wx.showToast({
        title: '图片上传中',
        icon: 'loading'
      })
    } else {
      if (this.data.submitFlag) {
        this.setData({
          submitFlag: false
        })
        wx.request({
          url: api.cloudAddQuestion,
          method: "POST",
          data: {
            moduleType: that.data.moduleType,
            content: that.data.title,
            userId: wx.getStorageSync('workNo'),
            imglist: that.data.imgUrl,
            pagepath: '/pages/feedbackDetailReply/feedbackDetailReply?idx=idx&feedbackId='
          },
          header: {
            'content-Type': 'application/json;charset=UTF-8',
            'token': wx.getStorageSync('token')
          },
          success(e) {
            if (e.data.code == "000000") {
              wx.showToast({
                title: '发布成功',
              })
              wx.navigateBack({
                delta: 1,
              })
            } else {
              wx.showToast({
                title: '发布失败',
                icon: 'error'
              })
            }
            that.setData({
              submitFlag: true
            })
          },
        })
      }
    }
  },
})